package com.io.util;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.Map;

/**
 * The class/interface
 *
 * @author guodd
 * @version 1.0 use jdk 1.8
 */
public class GetCameraPreviewURL {

    public static String getViewURL(Map<String, Object> mapValue) {

        /**
         * STEP1：设置平台参数，根据实际情况,设置host appkey appsecret 三个参数.
         */
        // ArtemisConfig.host = "120.46.140.215"; // 平台的ip端口
        // ArtemisConfig.appKey = "24360240";  // 密钥appkey
        // ArtemisConfig.appSecret = "xkvtgq4Ijeg3goiDRNRe";// 密钥appSecret

        /**
         * STEP2：设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = "/artemis";

        /**
         * STEP3：设置接口的URI地址，如：/api/video/v1/cameras/previewURLs
         */
        final String previewURLsApi = ARTEMIS_PATH + mapValue.get("url");
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                // 根据现场环境部署确认是http还是https
                put("https://", previewURLsApi);
            }
        };

        /**
         * STEP4：设置参数提交方式
         */
        String contentType = "application/json";

        /**
         * STEP5：组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        Object params = mapValue.get("params");
        if (!ObjectUtils.isEmpty(params)) {
            Map<Object, Object> map = MapUtil.createMap(params.getClass());
            System.out.println(map);
        }
        jsonBody.put("cameraIndexCode", "ed24fcd5741949b2a41ffdf97e76ff39");
        jsonBody.put("streamType", 0);
        // hls & rtsp
        jsonBody.put("protocol", "rtsp");
        jsonBody.put("transmode", 1);
        jsonBody.put("expand", "streamform=ps");
        String body = jsonBody.toString();
        /**
         * STEP6：调用接口
         */
        // post请求application/json类型参数
        return ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType, null);
    }
}
